<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class roles extends My_Controller {

    var $name = 'menu';
    
    public function __construct()
    {
        parent::__construct();
        //$this->load->model('menu_model');
    }
    
    public function index()
    {
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'roles'));
       $this->template->parse_view('content', 'default/roles/roles_index', array());
       // Render template
        $this->template->render();
    }
    
    function list_role($page = 1)
    {
        $this->load->model('roles_model');
        
        $itemsPerPage=20;
        $space=5;
        if($page == 0){$page=1;}
        
        
        $start = ($page - 1) * $itemsPerPage;
        $items = $this->roles_model->getAllListRoles($itemsPerPage,$start);
        $totalItems = $this->roles_model->countAllListRoles();
        $totalPages = round($totalItems / $itemsPerPage);
        
        if( $totalItems%$itemsPerPage != 0 ){
            $totalPages = ($totalItems - ( $totalItems%$itemsPerPage ) ) /$itemsPerPage + 1;
        }
        
        $this->load->view('default/roles/ajax_list_roles',array(
            'items'=> $items,
            'itemsPerPage'=>$itemsPerPage,
            'page'=>$page,
            'totalPages'=>$totalPages,
            'space' => $space
        ));
    }
    
    function insert(){
        $this->load->model('roles_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('name', 'Role Name', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('description', 'Description', 'required|max_length[255]|');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');

            }else{

                $name = $this->input->post('name');
                $description = $this->input->post('description');
                $candelete = $this->input->post('candelete');
                
                $insertdata = array(
                    'role_name' => $name,
                    'description' => $description,
                    'default' => 0,
                    'can_delete' => $candelete,
                    'deleted' => 0
                );
                $new_roleId = $this->roles_model->insertRole($insertdata);
                
                redirect('roles', 'location', 301);
            }
        }
        
        $data_sidebar = array(
            'menu' => 'roles'
        );
        $data = array();
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
        $this->template->parse_view('content', 'default/roles/insert_role', array(
            'action' => site_url('roles/insert'),
            'page_type' => 'insert'
        ));
       // Render template
        $this->template->render();
        
    }
    
    function edit($roleId = 0){
        $this->load->model('roles_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $role = $this->roles_model->getRoleById($roleId);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('name', 'Role Name', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('description', 'Description', 'required|max_length[255]|');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');
                
            }else{
                $name = $this->input->post('name');
                $description = $this->input->post('description');
                $candelete = $this->input->post('candelete');
                
                $updatetdata = array(
                    'role_name' => $name,
                    'description' => $description,
                    'can_delete' => $candelete
                );
                $this->roles_model->updateRole($roleId,$updatetdata);
                
                redirect('roles', 'location', 301);
            }
        }
        //xu ly datatable permission
        $data_active = $this->roles_model->getAllPermissionByRoleId($roleId);
        
        $list_permission_active = array();
        if($data_active){
            foreach($data_active As $item){
                $list_permission_active[] = $item->permission_id;
            }
        }
        
        $data = $this->roles_model->loadPermission();
        //var_dump($data);
        $table_data = array();
        
        foreach($data As $key=>$item){
            $col_header = array();
            $row_item = array();
            $i = 0;
            
            $row_item['0header0'][$i] = '';
            foreach($item As $row){
                $row_item[$row['row']][$row['col']] = array('permission_id' => $row['permission_id'],'checked' => ( in_array($row['permission_id'],$list_permission_active) ) ? true :false 
                    );
                if( !in_array($row['col'],$row_item['0header0']) ){
                    $row_item['0header0'][$i++] = $row['col'];
                }
                
            }
            //$row_item[$key][] = $col_header;
            $table_data[$key] = $row_item;
        }
        
        $datatablePermission_html = $this->load->view('default/roles/theme_table_permission',array('table_data'=>$table_data,'roleId'=>$roleId),true);
        //end
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'roles'));
        $this->template->parse_view('content', 'default/roles/insert_role', array('datatablePermission_html'=>$datatablePermission_html,
            'action' => site_url('roles/edit/'.$roleId),
            'page_type' => 'edit',
            'role'=>$role
        ));
       // Render template
        $this->template->render(); 
    }

    function setPermission($roleId, $permissionId, $status){
        $this->load->model('roles_model');
        //echo $status;
        switch($status){
            case 'set':
                $data_insert = array('role_id'=>$roleId,'permission_id'=>$permissionId);
                $this->roles_model->insertRolePermission($data_insert);
            break;
            
            case 'unset':
                $this->roles_model->deleteRolePermission($roleId, $permissionId);
            break;
        }
        
        $this->load->view('default/roles/ajax_change_permission',array('role_id'=>$roleId,'permission_id'=>$permissionId,'status'=>$status));
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */